<?php

class NewsController extends App_Controller_Action { 
/**
     * Отображает главную страницу
     */
    public function indexAction() 
    {
        $page = $this->_getParam('page');
    	$modelNews = new News();
    	$news = $modelNews->getNews();
        $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_Array($news));
        $paginator->setCurrentPageNumber($page);
        $this->view->news = $paginator;
    }
    
    public function defaultAction() {
    	$this->_helper->redirector->gotoRoute(array(), 'news');
    }

    /**
     * Страница из меню
     */    
    public function viewAction() 
    {
        $newId = $this->_getParam('newId');
        $modelNews = new News();
        $new = $modelNews->getNew($newId);
        $this->view->new = $new;
    }
    
    public function adminlistAction() {
    	if (!$this->isAuth()) {	$this->_helper->redirector->gotoRoute(array(), 'adminLogin'); }
    	$modelNews = new News();
    	$news = $modelNews->getNews();
        $this->view->news = $news;
    }
    
    public function adminformAction() {
    	if (!$this->isAuth()) {	$this->_helper->redirector->gotoRoute(array(), 'adminLogin'); }
    	if ($this->_request->isPost()) {
    		$formNew = new FormNews('');
    		if ($formNew->isValid($this->_getAllParams())) {
    			$post = $this->_request->getPost();
    			$new = new News();
    			$new->setDataFromArray($post);
    			$new->bdate = time();
    			if ($new->id) {
	    			$new->update();
	    		} else {
    				$new->insert();
	    		}
	    		$this->_helper->redirector->gotoRoute(array(), 'adminNews');
    		}
			$this->view->formNews = $formNew;
    	} else {
    		$id = $this->_getParam('newId');
        	$modelNews = new News();
        	$new = $modelNews->getNew($id);
			$formNews = new FormNews($new);
			$this->view->formNews = $formNews;
    	}
    }

    public function admindeleteAction() {
    	if (!$this->isAuth()) {	$this->_helper->redirector->gotoRoute(array(), 'adminLogin'); }
    	$id = $this->_getParam('newId');
        $modelNews = new News();
        $res = $modelNews->delete('ID='.$id);
		$this->_helper->redirector->gotoRoute(array(), 'adminNews');
    }
}
